package com.ruoyi.project.xkx.school.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.xkx.school.domain.XkxCourse;
import com.ruoyi.project.xkx.school.service.IXkxCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.xkx.school.mapper.XkxSchoolCourseMapper;
import com.ruoyi.project.xkx.school.domain.XkxSchoolCourse;
import com.ruoyi.project.xkx.school.service.IXkxSchoolCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ${author}
 * @date 2021-05-10
 */
@Service
public class XkxSchoolCourseServiceImpl extends ServiceImpl<XkxSchoolCourseMapper, XkxSchoolCourse> implements IXkxSchoolCourseService {
  @Autowired
  private XkxSchoolCourseMapper xkxSchoolCourseMapper;
  @Autowired
  private IXkxSchoolCourseService schoolCourseService;

  @Autowired
  private IXkxCourseService courseService;

  @Override
  @Transactional
  public boolean updateSchoolCourse(Integer schoolId, List<Integer> courseIds) {
    boolean school_id = schoolCourseService.remove(new QueryWrapper<XkxSchoolCourse>().eq("school_id", schoolId));
    if (courseIds.size()>0){
      for (Integer courseId : courseIds) {
        XkxSchoolCourse xkxSchoolCourse =new XkxSchoolCourse();
        xkxSchoolCourse.setSchoolId(schoolId);
        xkxSchoolCourse.setCourseId(courseId);
        xkxSchoolCourseMapper.insert(xkxSchoolCourse);
      }
    }
    return true;
  }
  @Override
  @Transactional
  public Map getSchoolCourse(Integer schoolId) {
    HashMap<String,Object> stringObjectHashMap = new HashMap<>();
    //获取校区课程id
    List<XkxSchoolCourse> schoolCourseList = schoolCourseService.list(new QueryWrapper<XkxSchoolCourse>().eq("school_id" , schoolId));
    List<Integer> course = new ArrayList<>();
    for (XkxSchoolCourse xkxSchoolCourse : schoolCourseList) {
      course.add(xkxSchoolCourse.getCourseId());
    }
    //获取校区课程信息
    List<XkxCourse> courses= new ArrayList<>();
    if (course.size()>0){
      courses = courseService.list(new QueryWrapper<XkxCourse>().in( "course_id" , course));
    }
    stringObjectHashMap.put("schoolCourse",courses);
    List<SysUser> schoolUser = xkxSchoolCourseMapper.getSchoolUser(schoolId);
    stringObjectHashMap.put("schoolTeacher",schoolUser);
    return stringObjectHashMap;
  }
}
